//
//  VisitorLoginView.swift
//  vveboSwiftPro
//
//  Created by 杨笑怡 on 15/6/28.
//  Copyright © 2015年 YangXiaoYi. All rights reserved.
//

import UIKit
import FFAutoLayout

///  访客视图协议 - 协议需要继承自 NSObjectProtocol
protocol VisitorLodinViewDelegate: NSObjectProtocol {
    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()
}

class VisitorLoginView: UIView {
    
    /// 代理 weak id<VisitorLodinViewDelegate> delegate
    weak var delegate: VisitorLodinViewDelegate?

    ///  设置视图内容
    ///
    ///  * iconName ：图标名称
    ///  * message ： 消息内容
    ///  * isHome ：  是否首页
    func setupViewInfo(iconName: String ,message: String, isHome: Bool) {
        messageLabel.text = message
        
        if isHome {
            homeIconView.hidden = false
            registerButton.hidden = true
            loginButton.hidden = true
            concernButton.hidden = false
            smallIconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")

        } else {
            homeIconView.hidden = true
            registerButton.hidden = false
            loginButton.hidden = false
            concernButton.hidden = true
            smallIconView.image = UIImage(named: iconName)
            // 将视图层次置后
            sendSubviewToBack(iconMaskView)

        }
        
    }
    ///  开始动画
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 25.0
        
        smallIconView.layer.addAnimation(anim, forKey: nil)
    }
    
    ///  用户注册
    func register() {
        delegate?.visitorRegisterButtonClicked()
    }
    ///  用户登录
    func login() {
        delegate?.visitorLoginButtonClicked()
    }
    ///  关注
    func concern() {
        print("userConcern")
    }
    
    // 视图的默认构造方法
    // 创建控件，并且指定布局。用xib & sb 开发不会调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI() 
    }

    // 解档， 用xib & sb 开发会调用
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 创建控件
    private func setupUI() {
        // 1. 添加控件
        addSubview(smallIconView)
        addSubview(iconMaskView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(concernButton)
        
        // 2. 自动布局
        // 任何一个控件，都可以参照另外一个控件准确的设置位置
        smallIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -60))
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: smallIconView, size: nil)
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: smallIconView, size: CGSize(width: 240, height: 40), offset: CGPoint(x: 0 , y: 20))
        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0 ,y: 20))
        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0 ,y: 20))
        concernButton.ff_AlignVertical(ff_AlignType.BottomCenter, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0 ,y: 20))
        
        iconMaskView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom: 160, right: 0))
    
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
    // MARK: - 懒加载控件
    /// 转盘图标
    lazy var smallIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()

    /// 遮罩图标
    lazy var iconMaskView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    
    /// 页面图标
    lazy var homeIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()

    /// 文字标签
    lazy var messageLabel: UILabel = {
        let label = UILabel(frame: CGRectMake(0, 0, 240, 40))
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.preferredMaxLayoutWidth = 224
        label.numberOfLines = 2
        
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    lazy var registerButton: UIButton = {
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 40))
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    /// 登陆按钮
    lazy var loginButton: UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 0, 100, 40))
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    /// 关注按钮
    lazy var concernButton: UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 200, 100, 40))
        
        btn.setTitle("去关注", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "concern", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
}
